package org.deng.vertx.springboot.verticle.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.vertx.core.eventbus.Message;
import lombok.extern.slf4j.Slf4j;
import org.deng.vertx.springboot.entity.SysUser;
import org.deng.vertx.springboot.msg.MsgAddress;
import org.deng.vertx.springboot.pojo.PageBo;
import org.deng.vertx.springboot.pojo.PageVo;
import org.deng.vertx.springboot.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 邓力宾
 * @date 2022/12/29 16:43
 * @desc
 */
@Component
@Slf4j
public class SysUerServiceVerticle extends BaseServiceVerticle  {
    private ISysUserService sysUserService;

    @Autowired
    public void setSysUserService(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @Override
    public void start() throws Exception {
        log.info("ServiceVerticle start.......");
        //接收消息
        vertx.eventBus().consumer(MsgAddress.userServiceGetById, this::getById);
        vertx.eventBus().consumer(MsgAddress.userServiceGetPage, this::getPage);
    }

    private  void getPage(Message<PageBo> tMessage) {
        PageBo pageBo = tMessage.body();

        IPage<SysUser> page = sysUserService.getPage(pageBo.getPage(), pageBo.getSize());
        tMessage.reply(PageVo.fromPage(page));
    }

    private void  getById(Message<String> idMsg){
        String id = idMsg.body();
        //log.info("收到消息:{}, id:{}", MsgAddress.userServiceGetById, id);
        SysUser user = sysUserService.getById(id);
        //发送消息
        idMsg.reply(user);
    }


}
